"""
接口定义
"""
from fastapi import APIRouter, Query, Request, Depends, Path, Form
from pydantic import BaseModel
from app.response import success, fail
from app.cache import get_redis
from app.database import get_db
import time

api_router = APIRouter(prefix='/api')


@api_router.get("/index/{username}", tags=['测试接口'])
def index(
    page:int = Query(gt=1), # 查询参数
    username: str = Path(min_length=1) #路径参数

):
    """
    Hello world接口
    :return:
    """

    return success("success", [page, username])

class JsonData(BaseModel):
    x: int
    y: int

    
@api_router.post("/index2", tags=['测试接口'])
def test2(
    d: JsonData, #json body 参数
    x: int = Form(...) #表单参数
):

    return success("OK", d)

@api_router.get("/index3", tags=['测试接口'])
def test1(rd=Depends(get_redis)):
    """
    redis测试接口
    :param rd:
    :return:
    """
    rd.set("Test1", "%d" % time.time(), 100)
    r = rd.get("Test1")
    return success("OK", rd.get("Test1"))


@api_router.get("testDb", tags=['测试接口'])
def test2(db=Depends(get_db)):
    """
    数据库操作测试接口
    :param db:
    :return:
    """
    data = db.getAll("select id, name from bx_area where pid=1532")
    db.query(
        """
            update bx_area set name='zzz' where id=10000
        """
    )

    return success("OK", data)
